home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ptv3n5.zip / FPSPEED.CPP < prev    next >
C/C++ Source or Header  |  1992-09-20  |  4KB  |  147 lines

  1. /*========================================================================
  2.  fpspeed.cpp  --  Timing benchmarks for fpnum class  [Listing #4]
  3.  by Robert N. Goldrich
  4.                     In order to             In order to
  5.  Compiler           Use x87 Chip            not use x87
  6. --------------------------------------------------------------------------
  7.  Borland            <default>               set 87=n
  8.  Microsoft
  9.  Zortech            <default>               extern int _8087=0 ;
  10.  Topspeed
  11. ========================================================================*/
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include "fpnum.h"
  16.  
  17. #define SEED   12345
  18.  
  19. inline int Random(int n)
  20. {
  21.     return (int) (((long)rand() * n ) /(RAND_MAX+1)) ;
  22. }
  23.  
  24. main( int argc, char *argv[] )
  25. {
  26. #if defined(__ZTC__) && defined( NO87 )
  27. extern int _8087 ;  _8087=0 ;
  28. #endif
  29.  
  30. int loops = ( argc>1 ) ? atoi( argv[1] )  :  10000 ;
  31.  
  32. clock_t  start, stop ;
  33. int      count ;
  34. fpnum    a, b, x ;
  35. double   ad, bd, xd ;
  36. double   overhead, fpdiv, fpmul, fpadd, ddiv, dmul, dadd ;
  37.  
  38. //-- FIXED POINT OVERHEAD --------------------------------
  39. srand( SEED ) ;
  40. start = clock() ;
  41. for( count=0; count<loops; count++ ) {
  42.     long temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  43.     a.poke( temp ) ;
  44.     temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  45.     b.poke( temp ) ;
  46. }
  47. stop = clock() ;
  48. overhead = ( stop - start ) / CLK_TCK ;
  49. printf("Fixed point overhead = %lf seconds \n", overhead ) ;
  50.  
  51. //-- FIXED POINT MULT
  52. srand( SEED ) ;
  53. start = clock() ;
  54. for( count=0; count<loops; count++ ) {
  55.     long temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  56.     a.poke( temp ) ;
  57.     temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  58.     b.poke( temp ) ;
  59.     x = a * b ;
  60. }
  61. stop = clock() ;
  62. fpmul = ( stop - start - overhead ) / CLK_TCK ;
  63.  
  64. //-- FIXED POINT DIVIDE
  65. srand( SEED ) ;
  66. start = clock() ;
  67. for( count=0; count<loops; count++ ) {
  68.     long temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  69.     a.poke( temp ) ;
  70.     temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  71.     b.poke( temp ) ;
  72.     x = a / b ;
  73. }
  74. stop = clock() ;
  75. fpdiv = ( stop - start - overhead ) / CLK_TCK ;
  76.  
  77. //-- FIXED POINT ADD
  78. srand( SEED ) ;
  79. start = clock() ;
  80. for( count=0; count<loops; count++ ) {
  81.     long temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  82.     a.poke( temp ) ;
  83.     temp = ( (long)(Random(180)+1) << 16 ) | rand() ;
  84.     b.poke( temp ) ;
  85.     x = a + b ;
  86. }
  87. stop = clock() ;
  88. fpadd = ( stop - start - overhead ) / CLK_TCK ;
  89.  
  90. //-- DOUBLE PRECISION OVERHEAD --------------------------------
  91. srand( SEED ) ;
  92. start = clock() ;
  93. for( count=0; count<loops; count++ ) {
  94.     ad = Random(180) + (double)rand()/FP_ONE + 1 ;
  95.     bd = Random(180) + (double)rand()/FP_ONE + 1 ;
  96. }
  97. stop = clock() ;
  98. overhead = ( stop - start ) / CLK_TCK ;
  99. printf("Double prec overhead = %lf seconds \n", overhead ) ;
  100.  
  101. //-- DOUBLE PREC MULT
  102. srand( SEED ) ;
  103. start = clock() ;
  104. for( count=0; count<loops; count++ ) {
  105.     ad = Random(180) + (double)rand()/FP_ONE + 1 ;
  106.     bd = Random(180) + (double)rand()/FP_ONE + 1 ;
  107.     xd = ad * bd ;
  108. }
  109. stop = clock() ;
  110. dmul = ( stop - start - overhead ) / CLK_TCK ;
  111.  
  112. //-- DOUBLE PREC DIVIDE
  113. srand( SEED ) ;
  114. start = clock() ;
  115. for( count=0; count<loops; count++ ) {
  116.     ad = Random(180) + (double)rand()/FP_ONE + 1 ;
  117.     bd = Random(180) + (double)rand()/FP_ONE + 1 ;
  118.     xd = ad / bd ;
  119. }
  120. stop = clock() ;
  121. ddiv = ( stop - start - overhead ) / CLK_TCK ;
  122.  
  123. //-- DOUBLE PREC ADD
  124. srand( SEED ) ;
  125. start = clock() ;
  126. for( count=0; count<loops; count++ ) {
  127.     ad = Random(180) + (double)rand()/FP_ONE + 1 ;
  128.     bd = Random(180) + (double)rand()/FP_ONE + 1 ;
  129.     xd = ad + bd ;
  130. }
  131. stop = clock() ;
  132. dadd = ( stop - start - overhead ) / CLK_TCK ;
  133.  
  134. //-- Tally the results
  135. printf("-----------------------------------------\n" ) ;
  136. printf("    Stats for %d operations:\n", loops ) ;
  137. printf("-----------------------------------------\n" ) ;
  138. printf("        fpnum    double   fpnum/double\n") ;
  139. printf("%5s  %7.3lf  %7.3lf    %7.4lf\n",  "Add",
  140.                 fpadd, dadd, fpadd/dadd ) ;
  141. printf("%5s  %7.3lf  %7.3lf    %7.4lf\n", "Mult",
  142.                 fpmul, dmul, fpmul/dmul ) ;
  143. printf("%5s  %7.3lf  %7.3lf    %7.4lf\n",  "Div",
  144.                 fpdiv, dadd, fpdiv/ddiv ) ;
  145. return 0 ;
  146. }
  147.